package com.guigu.utils;

import com.guigu.OrderService;
import com.guigu.pojo.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

@Component
public class NumberUtils {
    @Autowired
    private OrderService orderService;
    /**
     *
     *  * 生成订单编号
     *  * 要求：100+当前日期+4位流水号
     * @param
     * @return
     */
    public String setOrderId(String type){
        StringBuffer Buffer = new StringBuffer(type);
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        String temp= format.format(date);
        Buffer.append(temp);

        String OrderId="";


        switch (type){
            case "100":
                Order order = orderService.selectMaxId();
                if (order!=null){
                OrderId = order.getOrderId();
            }else {
                OrderId= null;
            }
                break;
        }
        String DesignIdLast="0001";
        if (!Objects.equals(null,OrderId) ){
            String data1=OrderId.substring(3,7);
            String data2=date.getYear()+1900+"";
            if (Objects.equals(data1,data2)){
                int tmp= Integer.parseInt(OrderId.substring(11))+1;
                DesignIdLast = String.format("%04d", tmp);
            }
        }
        OrderId=Buffer.append(DesignIdLast).toString();
        return OrderId;
    }

}
